package com.myapplication.ui.myactivity;

import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.myapplication.R;

import java.io.File;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UploadFileListener;

/**
 * Created by zitaojiang on 2016/10/19.
 */
public class RegisterActivity extends AppCompatActivity {
    @Bind(R.id.btn)
    Button btn;
    @Bind(R.id.btn_pic)
    Button btnPic;
    private String name;
    private String password;
    private MyUser myUser;
    private static final int PHOTO_REQUEST = 1;
    private  String url;
    //private BmobUser bmobUser;

    @Bind(R.id.et_register_name)
    EditText etRegisterName;
    @Bind(R.id.layout_register_name)
    TextInputLayout layoutRegisterName;
    @Bind(R.id.et_register_password)
    EditText etRegisterPassword;
    @Bind(R.id.layout_register_password)
    TextInputLayout layoutRegisterPassword;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        myUser=new MyUser();
        initView();
        init();
        btnPic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                    Intent intent = new Intent(Intent.ACTION_PICK);
                    intent.setType("image/*");
                    startActivityForResult(intent, PHOTO_REQUEST);

            }
        });

    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == PHOTO_REQUEST) {
            if (data != null) {
                Uri uri = data.getData();
                Log.i("taggg", "onActivityResult: "+uri.toString());
                //获得图片的路径
                String[] proj= {MediaStore.Images.Media.DATA};
                Cursor cursor=managedQuery(uri,proj,null,null,null);
                int column_index=cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                String path=cursor.getString(column_index);
                Log.i("tagggg", "onActivityResult: "+path);

                //上传图片
              //File file = new File("/mnt/sdcard"+path);
              File file = new File(path);
                Log.i("taggg", "onActivityResult: "+uri.toString());
               // Log.i("taggg", "onActivityResult: "+"/mnt/sdcard"+path);
                final BmobFile icon = new BmobFile(file);
               // myUser = new MyUser();
               // myUser.setIcon(icon);

                icon.uploadblock(new UploadFileListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e==null){
                            url=icon.getFileUrl();
                            //bmobFile.getFileUrl()--返回的上传文件的完整地址
                            Toast.makeText(RegisterActivity.this,"上传文件成功:"+ icon.getFileUrl(),Toast.LENGTH_LONG).show();

                        }else{
                            Toast.makeText(RegisterActivity.this,"上传文件失败:"+e.getMessage(),Toast.LENGTH_LONG).show();
                        }
                    }
                });

               /* bmobFile.upload(new UploadFileListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e==null){
                            //bmobFile.getFileUrl()--返回的上传文件的完整地址
                            Toast.makeText(RegisterActivity.this,"上传文件成功:"+ bmobFile.getFileUrl(),Toast.LENGTH_LONG).show();

                    }else{
                        Toast.makeText(RegisterActivity.this,"上传文件失败:"+e.getMessage(),Toast.LENGTH_LONG).show();
                    }
                    }

                    @Override
                    public void onProgress(Integer value) {
                        super.onProgress(value);
                    }
                });
*/
            }
        }
        super.onActivityResult(requestCode, resultCode, data);

    }

    private void initView() {
        etRegisterName = layoutRegisterName.getEditText();
        etRegisterName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence.length() < 5) {
                    layoutRegisterName.setErrorEnabled(true);
                    layoutRegisterName.setError("请输入正确邮箱");
                } else {
                    layoutRegisterName.setErrorEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {


            }
        });
        etRegisterPassword = layoutRegisterPassword.getEditText();
        etRegisterPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence.length() < 6) {
                    layoutRegisterPassword.setErrorEnabled(true);
                    layoutRegisterPassword.setError("请输入不少于6位密码");
                } else {
                    layoutRegisterPassword.setErrorEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

    }

    private void init() {
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                name = etRegisterName.getText().toString();
                password = etRegisterPassword.getText().toString();
                if (name.isEmpty() || password.isEmpty()) {
                    Toast.makeText(RegisterActivity.this, "账号或者密码不为空", Toast.LENGTH_LONG).show();

                    return;
                } else {
                   // myUser = new MyUser();
                    BmobUser bmobUser=new BmobUser();
                    bmobUser.setUsername(name);
                    bmobUser.setPassword(password);
                    //bmobUser.setEmail(url);
                   // Toast.makeText(RegisterActivity.this, "注册成功！"+url, Toast.LENGTH_SHORT).show();

                    bmobUser.signUp(new SaveListener<MyUser>() {
                        @Override
                        public void done(MyUser myUser, BmobException e) {
                           Toast.makeText(RegisterActivity.this, "注册成功！", Toast.LENGTH_SHORT).show();
                                Intent intent_Login=new Intent(RegisterActivity.this,LoginActivity.class);
                                startActivity(intent_Login);
                            finish();

                        }
                    });

                  /*  myUser.signUp(new SaveListener<MyUser>() {
                        @Override
                        public void done(MyUser myUser, BmobException e) {
                            if (e == null) {
                                Toast.makeText(RegisterActivity.this, "注册成功！", Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(RegisterActivity.this, "注册失败，请检查网络是否连接！", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });*/
                  /*  myUser.save(new SaveListener<String>() {
                        @Override
                        public void done(String s, BmobException e) {
                            if (e == null) {
                                Toast.makeText(RegisterActivity.this, "注册成功！", Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(RegisterActivity.this, "注册失败，请检查网络是否连接！", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });*/
                }

            }
        });


    }


}
